package com.fedming.bottomnavigationdemo.Fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.fedming.bottomnavigationdemo.Adapter.DoctorAdvice;
import com.fedming.bottomnavigationdemo.Adapter.DoctorAdviceRecycleViewAdapter;
import com.fedming.bottomnavigationdemo.HomeActivity;
import com.fedming.bottomnavigationdemo.R;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class DoctorAdviceFragment extends BaseLazyFragment {
    private RecyclerView recyclerView_advice;
    private List<String> adviceList = new ArrayList<>();
    private DoctorAdviceRecycleViewAdapter recycleviewadapter;
    private OkHttpClient client;

    public TextView office3;
    public TextView admission_number3;
    public TextView name3;
    public TextView doctor_name2;
    public TextView nurse_name2;
    public TextView gender3;
    public TextView age2;
    public TextView location_code3;

    @Nullable
    @Override  //
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.doctor_advice_fragment2,container,false);
        //initAdvice();
        office3 = (TextView)view.findViewById(R.id.office3);
        admission_number3 = (TextView)view.findViewById(R.id.admission_number3);
        name3 = (TextView)view.findViewById(R.id.name3);
        doctor_name2 = (TextView)view.findViewById(R.id.doctor_name2);
        nurse_name2 = (TextView)view.findViewById(R.id.nurse_name2);
        gender3 = (TextView)view.findViewById(R.id.gender3);
        age2 = (TextView)view.findViewById(R.id.age2);
        location_code3 = (TextView)view.findViewById(R.id.location_code3);

        recyclerView_advice= (RecyclerView) view.findViewById(R.id.recyclerview_advice);
        mIsprepared = true;
        lazyLoad();
        Log.e(TAG,"onCreateView");
        return view;

    }
    public DoctorAdviceFragment(){
        TAG = "DoctorAdviceFragment";
    }

    private void loadData(){
        DoctorAdvice a1 = new DoctorAdvice("1月11日","医嘱， 就是医生根据病情和" +
                "治疗的需要对病人在饮食、用药、化验等方面的指示。医嘱是指医师在医疗活动中下达的医学指令" +
                "。医嘱内容及起始、停止时间应当由医师书写。医嘱内容应当准确、清楚，每项医嘱应当只包含一个" +
                "内容，并注明下达时间，应当具体到分钟。" +
                "",R.mipmap.news_icon,"医嘱通知");
        recycleviewadapter.notifyDataSetChanged();//通知数据已更新，刷新recycleview
    }

    @Override
    protected void lazyLoad() {
        if (!mIsprepared || !mIsVisible || mHasLoadedOnce) {
            return;
        }
        mHasLoadedOnce = true;
        //UI和业务逻辑
        Log.e(TAG,"加载"+TAG+"的UI");
        //getAsyn("http://10.181.255.203:8080//bed/index?id=1");

    }

    public void InitData(){
        office3.setText(HomeActivity.office);
        admission_number3.setText(HomeActivity.pno);
        name3.setText(HomeActivity.name);
        doctor_name2.setText(HomeActivity.doctor);
        nurse_name2.setText(HomeActivity.nurse);
        String gender_string = "男";
        if(HomeActivity.gender =="f")
            gender_string = "女";
        gender3.setText(gender_string);
        age2.setText(HomeActivity.age+"岁");
        location_code3.setText(HomeActivity.room+"-"+HomeActivity.code);

    }
    //异步get请求
    public void getAsyn(String url) {
        client = new OkHttpClient.Builder()
                .connectTimeout(5000, TimeUnit.MILLISECONDS)
                .readTimeout(20000, TimeUnit.MILLISECONDS)
                .writeTimeout(20000, TimeUnit.MILLISECONDS)
                .build();
        Request request = new Request.Builder().url(url).build();
        Call call = client.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                //...
                Log.e(TAG,"http请求失败");
                Log.e(TAG,e.toString());
                getActivity().runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        Toast toast = Toast.makeText(getActivity(), "数据刷新失败，请检查网络连接", Toast.LENGTH_SHORT);
                        toast.setGravity(Gravity.CENTER, 0, 0);
                        toast.show();
                    }
                });

            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                if(response.isSuccessful()){
                    String result = response.body().string();
                    //处理UI需要切换到UI线程处理
                    getActivity().runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            loadData();
                        }
                    });
                }
            }
        });
    }
    @Override
    public void onStart() {
        super.onStart();
        InitData();
        adviceList.clear();
        int size = HomeActivity.advices.size();
        for(int i=0;i<size;i++){
            JSONObject obj = JSON.parseObject(HomeActivity.advices.get(i).toString());
            adviceList.add(obj.getString("content"));
        }

        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity());
        recyclerView_advice.setLayoutManager(linearLayoutManager);
        recycleviewadapter = new DoctorAdviceRecycleViewAdapter(adviceList);//adviceList改变了Adapter里的mAdviceList也会改变
        recyclerView_advice.setAdapter(recycleviewadapter);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        mHasLoadedOnce = false;
        mIsprepared = false;
        adviceList.clear();
        Log.e(TAG,"onDestroy");
    }


}
